package dao.Implementation;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import Donnees.OptionHebergement;
import Util.HibernateUtil;
import dao.Interfaces.IOptionHebergementDAO;

public class OptionHebergementDAO implements IOptionHebergementDAO {
	public OptionHebergementDAO() {
	}
	
	public List<OptionHebergement> selectAll() {
		List<OptionHebergement> listeOptionHebergement = new ArrayList<OptionHebergement>();

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		listeOptionHebergement.addAll(session.createQuery("from OptionHebergement").list());

		session.flush();
		tx.commit();
		session.close();

		return listeOptionHebergement;
	}

	public long insert(OptionHebergement f) {
		OptionHebergement optionHebergementTemp = f;
		Long msgId;

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		msgId = (Long) session.save(optionHebergementTemp);

		session.flush();
		tx.commit();
		session.close();
		
		return msgId;
	}
	
	public int delete(OptionHebergement f) {
		OptionHebergement optionHebergementTemp = f;
		int retour;
		
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();
		
		retour = session.createQuery("delete from OptionHebergement where id='" + optionHebergementTemp.getId() + "'").executeUpdate();

		session.flush();
		tx.commit();
		session.close();
		
		return retour;
	}
	
	public OptionHebergement getById(long id) {
		OptionHebergement optionHebergementTemp = new OptionHebergement();

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		optionHebergementTemp = (OptionHebergement) session.createQuery("from OptionHebergement where id='" + id + "'").list().get(0);

		session.flush();
		tx.commit();
		session.close();

		return optionHebergementTemp;
	}
}
